@extends('layouts.header')
@section('title','带电作业系统-关系映射')
@section('content')
@php
    $thirdSiteName = $data['info']->name;
    $types = $data['dbStat']['type'];
@endphp
<nav class="breadcrumb"><i class="Hui-iconfont">&#xe67f;</i> 首页 <span class="c-gray en">&gt;</span> 系统对接 <span
        class="c-gray en">&gt;</span> {{$thirdSiteName}}
    <a class="btn btn-primary radius r" style="line-height:1.6em;margin-top:3px" href="javascript:location.replace(location.href);" title="刷新"><i class="Hui-iconfont">&#xe68f;</i></a>
</nav>
<article class="page-container">
    <div class="cl pd-5 bg-1 bk-gray">
        <b class="btn btn-primary radius" onclick="clearCache()">清除缓存</b>
        <a class="btn btn-primary radius" href="testItem?third_id={{request('third_id')}}">查看{{$thirdSiteName}}项目</a>
        <a class="btn btn-primary radius" onclick="showToolStd()">工器具类型--标准</a>
        <span class="c-red ml-20">以下数据回车保存</span>
    </div>
    <div id="tab_demo" class="HuiTab">
        <div class="tabBar clearfix">@foreach($types as $type)<span>{{$type}}</span>@endforeach</div>
        @foreach($types as $typeKey => $type)
            <div class="tabCon">
                <div class="mt-10">
                    <span class="r ml-20">
                        <b class="btn btn-primary radius size-S" onclick="saveTb(this)">保存表格</b>
                    </span>
                    <table class="table table-border table-bordered table-hover table-bg table-responsive datatable">
                        <thead>
                            <tr class="text-c">
                                <th>本系统-{{$type}}</th>
                                <th>
                                    {{$thirdSiteName}}-{{$type}}
                                    <br />
                                    <span class="c-red">多个值用英文逗号隔开</span>
                                </th>
                                <th class="hidden">本系统-ID</th>
                                <!-- 检测项目、检测标准、工器具类型 -->
                                @if($typeKey == 4 || $typeKey == 3 || $typeKey == 2) 
                                <th>
                                    {{$thirdSiteName}}-{{$type}}-ID
                                    <br />
                                    <span class="c-red">多个值用英文逗号隔开，与名称顺序保持一致</span>
                                </th>
                                @endif
                                <!-- 检测数据必须对应，其他没必要 -->
                                @if($typeKey == 5)
                                <th>
                                    倍数：本系统值 x 倍数 = {{$thirdSiteName}}值
                                    <br />
                                    <span class="c-red">上传数据单位不同必填</span>
                                </th>
                                <th>
                                    计算公式
                                </th>
                                @endif
                                <th>是否上传：1是；0否（默认1）</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($data['system'][$typeKey] as $sysId => $sysName)
                            @php  
                                if($typeKey == 0 || $typeKey == 1){ //额定电压和分类
                                    $sysId = 0;
                                }
                                try{
                                    //dd($data);
                                    $mapData = $data['third'][$typeKey][$sysName];
                                    $mapData['third_name'] = htmlspecialchars_decode($mapData['third_name']);
                                }catch(\Exception $e){
                                    $mapData = [
                                        'sys_id' => 0,
                                        'third_id2' => '',
                                        'third_name' => '',
                                        'times' => '',
                                        'upload' => 1,
                                        'formula' => '',
                                    ];
                                }
                            @endphp
                            <tr class="text-c">
                                <input type="hidden" name="type" value="{{$typeKey}}">
                                {{csrf_form_field()}}
                                <td>{{$sysName}}<input type="hidden" name="sys_name" value="{{$sysName}}"></td>
                                <td>
                                    <span class="hidden">{{$mapData['third_name']}}</span>
                                    <input type="text" name="third_name" value="{{$mapData['third_name']}}" class="input-text" style="width:80%;" />
                                </td>
                                <td class="hidden">
                                    {{$sysId}}<input type="hidden" name="sys_id" value="{{$sysId}}">
                                </td>
                                <!-- 检测项目、检测标准、工器具类型 -->
                                @if($typeKey == 4 || $typeKey == 3 || $typeKey == 2)
                                <td>
                                    <span class="hidden">{{$mapData['third_id2']}}</span>
                                    <input type="text" name="third_id2" value="{{$mapData['third_id2']}}" class="input-text">
                                </td>
                                @endif
                                <!-- 检测数据必须对应，其他没必要 -->
                                @if($typeKey == 5)
                                <td><input type="text" name="times" class="input-text" style="width:80%;" value="{{$mapData['times']}}" /></td>
                                <td><input type="text" name="formula" class="input-text" style="width:80%;" value="{{$mapData['formula']}}" /></td>
                                @endif
                                <td><input type="text" name="upload" class="input-text" style="width:80%;" value="{{$mapData['upload']}}" /></td>
                            </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
            </div>
        @endforeach
    </div>
    <div class="hidden" id="tool_stds">
        <table class="table table-border table-bordered table-hover table-bg table-responsive">
            <thead>
                <tr>
                    <th>工器具类型</th>
                    <th>检测标准</th>
                </tr>
            </thead>
            <tbody>
                @foreach($toolStds as $tool_name => $stds)
                    <tr>
                        <td>{{$tool_name}}</td>
                        <td>{{implode('，',$stds)}}</td>
                    </tr>
                @endforeach
            </tbody>
        </table>
    </div>
</article>

<script type="text/javascript">
    var types = @json($types);
    $(function () {
        $('.datatable').DataTable({
            order:[[1,'desc']]
        });
        $('.select2').select2();
        $('#refresh').trigger('click');
        $("#tab_demo").Huitab({
            index: localStorage.getItem('thirdMapIndex',0),
        });
        $('.tabBar',"#tab_demo").on('click',function () {
            let index = 0,spans = $(this).children('span');
            spans.each(function () {
                if($(this).hasClass('current')){
                    localStorage.setItem('thirdMapIndex',index);
                }
                index += 1;
            })
        })
    })

    $('input').on('keypress',function(){
        if (event.keyCode == 13) {
            // console.log($(this));
            save(this);
        }
    })
    
    //保存
    function save(obj){
        let tr = $(obj).closest('tr'),
            data = {
                third_id: {{request('third_id')}},
            };
        tr.find('input').each(function(){
            data[$(this).attr('name')] = $(this).val();
        })
        data._form_token = '{{csrf_form_token()}}';
        ajax('post','',data);
    }

    //清空缓存
    function clearCache(){
        @php
            foreach($data['dbStat']['cache_key'] as $key){
                cache()->forget($key);
            }
        @endphp
        location.reload();
    }

    function showToolStd() {
        let content = $('div#tool_stds').html();
        layer_show();
        layer.open({
            type: 1,
            area:['80%','80%'],
            fix: false, //不固定
            shade:0.4,
            title: '工器具类型-标准',
            content: content,
            anim: 0,
            isOutAnim: false,//关闭层时的过度动画
            resize: true,//是否允许右下角拖动来拉伸
            moveOut: true,//是否允许拖拽到窗口外
        });
    }
    function saveTb(obj) {
        $(obj).closest('div').find('table tbody').find('tr').each(function () {
            save($(this).first('td'));
        })
    }
</script>
@endsection